import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/domain/domain_filter_item_model.dart';
import 'package:juming_test/widget/common_string_picker.dart';

/// 触发选择器组件
class DomainFilterPicker extends StatelessWidget {
  const DomainFilterPicker({
    super.key,
    required this.model,
    required this.onPick1Completed,
    required this.onPick2Completed,
  });

  // 数据源
  final DomainFilterItemModel model;
  // 事件
  // 输入完成回调
  final Function(String) onPick1Completed;
  final Function(String) onPick2Completed;

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Expanded(
          child: _buildArrowButton(true, () {
            _showPicker(context, (value) {
              onPick1Completed(value);
            });
          }),
        ),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 8.w),
          child: Text(
            '—',
            style: TextStyle(fontSize: 16.sp, color: Color(0xff333333)),
          ),
        ),
        Expanded(
          child: _buildArrowButton(false, () {
            _showPicker(context, (value) {
              onPick2Completed(value);
            });
          }),
        ),
      ],
    );
  }

  // 构建选择器按钮
  Widget _buildArrowButton(bool inLeft, Function() onTap) {
    String value = inLeft ? model.inputValue1 : model.inputValue2;
    return GestureDetector(
      onTap: () => onTap(),
      child: Container(
        height: 40.w,
        padding: EdgeInsets.symmetric(horizontal: 8.w),
        decoration: BoxDecoration(
          color: const Color(0xffF5F6F9),
          borderRadius: BorderRadius.circular(6),
        ),
        child: Row(
          children: [
            Expanded(
              child: Center(
                child: Text(
                  value.isEmpty ? '不限' : value,
                  style: TextStyle(fontSize: 16.sp),
                ),
              ),
            ),
            const Icon(Icons.keyboard_arrow_down, color: Color(0xff333333)),
          ],
        ),
      ),
    );
  }

  // 弹出选择器
  void _showPicker(BuildContext context, Function(String value) onPickConfirm) {
    final int from = model.pickData['from'] ?? 0;
    final int to = model.pickData['to'] ?? 0;
    final List<String> items = List.generate(
      to - from + 1,
      (index) => (from + index).toString(),
    );
    showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.zero),
      builder: (context) {
        return CommonStringPicker(
          pickItems: items,
          onPickConfirm: (value) => onPickConfirm(value),
        );
      },
    );
  }
}
